import 'package:pornhub_app/assets/styles.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/model/mine/resource_model.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/mine/favorite/controllers/favorite_page_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class ResourceItem extends GetView<FavoritePageController> {
  final ResourceModel resource;
  var index = 0;

  ResourceItem({
    super.key,
    required this.resource,
    required this.index,
  });

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return Column(
        children: <Widget>[
          Stack(
            children: <Widget>[
              _buildCover(),
              Positioned(top: 0.w, left: 0.w, child: _buildLabel()),
              _buildShadow(),
            ],
          ),
          SizedBox(width: 6.w),
          Expanded(
            child: _buildTitle(),
          ),
        ],
      );
    });
  }

  Widget _buildCover() {
    return ClipRRect(
      borderRadius: Styles.borderRaidus.xs,
      child: ImageView(
        src: resource.coverImg!,
        width: double.maxFinite,
        height: 90.w,
        fit: BoxFit.cover,
      ),
    );
  }

  Widget _buildLabel() {
    return Visibility(
      maintainSize: false,
      maintainAnimation: true,
      maintainState: true,
      maintainInteractivity: false,
      visible: resource.isUnlock!,
      child: Image.asset(
        'assets/mine/icon_resource_unlock.png',
        width: 37.w,
        height: 17.w,
      ),
    );
  }

  Widget _buildShadow() {
    return Visibility(
      maintainSize: false,
      maintainAnimation: true,
      maintainState: true,
      maintainInteractivity: false,
      visible: controller.visible.value,
      child: Stack(
        children: <Widget>[
          Container(
            width: double.maxFinite,
            height: 90.w,
            decoration: BoxDecoration(
              color: COLOR.hexColor('#80000000'),
              borderRadius: Styles.borderRadius.xs,
            ),
          ),
          Positioned(
            top: 5.w,
            right: 5.w,
            child: Image.asset(
              resource.isSelected!
                  ? 'assets/mine/icon_video_select.png'
                  : 'assets/mine/icon_video_unselect.png',
              width: 22.w,
              height: 22.w,
            ),
          ),
        ],
      ),
    ).onOpaqueTap(() {
      controller.onSelectedItem(index);
    });
  }

  Widget _buildTitle() {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 8.w),
      child: Text(
        resource.title ?? '',
        maxLines: 1,
        style: TextStyle(
          color: Styles.color.whiteText,
          fontSize: Styles.fontSize.sm,
          overflow: TextOverflow.ellipsis,
        ).copyWith(fontSize: 13.sp),
      ),
    );
  }
}
